package com.xiaomi.service.Impl;

import com.xiaomi.dao.*;
import com.xiaomi.pojo.*;
import com.xiaomi.service.OrderService;
import com.xiaomi.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderserviceImpl
 * @Author xiao宋
 * @Date 8:32  2022/9/16
 * @Version 1.0
 * @Description TODO
 */
@Service
public class OrderserviceImpl implements OrderService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    CartDao cartDao;
    @Autowired
    orderDetailDao orderDetailDao;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    AddressDao addressDao;


    @Override
    public Order addOrder(Integer aid, int uid) {
        List<Cart> carts = cartDao.getCart(uid);
        double sum = 0;
        //计算购物车总金额
        for (Cart cart : carts) {
            sum += cart.getMoney();
        }
        Order order = new Order();
        String orderId = RandomUtils.createActive();

        order.setId(orderId);
        order.setStatus("1");
        order.setAid(aid);
        order.setUid(uid);
        order.setMoney(sum);
        order.setTime(new Date());

        orderDao.inesert(order);

        for (Cart cart : carts) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOid(orderId);
            orderDetail.setPid(cart.getPid());
            orderDetail.setNum(cart.getNum());
            orderDetail.setMoney(cart.getMoney());

            orderDetailDao.insert(orderDetail);
        }
        //先注释掉清空购物车
        //       cartDao.clearCart(uid);

        return order;
    }

    public void updateOrderStatus(String orderId, String Status) {
        orderDao.upDateStatus(orderId, Status);
    }

    @Override
    public OrderDetail getOrderDetail(String oid) {
        OrderDetail od = new OrderDetail();
        List<OrderDetail> list = orderDetailDao.findOrderDetilByOid(oid);
        for (OrderDetail orderDetail : list) {
            Goods good = goodsDao.findById(orderDetail.getPid());
            orderDetail.setGoods(good);
        }

        od.setList(list);
        Order order = orderDao.findOrderById(oid);
        od.setOrder(order);
        Address address = addressDao.findAddressByAid(order.getAid());
        od.setAddress(address);
        return od;
    }

    @Override
    public List<Order> getUserAllOrderById(int uid) {
        List<Order> list = orderDao.getUserAllOrder(uid);
        for (Order order : list) {
            String name = orderDao.getUser(order.getUid()).getUsername();
            order.setUsername(name);
        }
        return list;
    }

    @Override
    public void sendOrder(String oid) {
        orderDao.sendOrder(oid);
    }

    @Override
    public List<Order> searchOrder(String username, String status) {
        return null;
    }

    @Override
    public List<Order> getOrderList(int uid) {
        return orderDao.getOrderList(uid);
    }

}
